<template>
  <div class="risk app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="红包口令"  prop="code"  >
        <el-input
          v-model="queryParams.code"
          placeholder="请输入红包口令"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型"  prop="type"  >
        <el-select v-model="queryParams.type" placeholder="红包类型" clearable>
          <el-option
            v-for="dict in dict.type.redp_packets_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="创建时间">
        <el-date-picker clearable
          v-model="queryParams.startCreateDate"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始时间"
          end-placeholder="结束时间">
         >
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%;">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="红包类型" align="center" prop="type" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.redp_packets_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="红包总额" align="center" prop="amt" />
      <el-table-column label="红包数量" align="center" prop="num" >
      </el-table-column>
      <el-table-column label="剩余数量" align="center" prop="remainNum" >
      </el-table-column>
      <el-table-column label="红包码" align="center" prop="code">
      </el-table-column>
      <el-table-column label="红包状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.redp_packets_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            :active-value="1"
            :inactive-value="2"
           @change="handleStatusChange(scope.row)"
         >
        </el-switch>
          <!-- <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['system:user:edit']">修改</el-button> -->
        </template>
      </el-table-column>
      <div class=""></div>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 新增红包 -->
    <el-dialog :title="title" :visible.sync="open" width="590px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="红包类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择红包类型" clearable>
                <el-option v-for="dict in dict.type.redp_packets_type" :key="dict.value" :label="dict.label"
                  :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="红包总额" prop="amt">
              <el-input type="number" v-model="form.amt" placeholder="请输入红包总额" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="红包数量" prop="num">
              <el-input type="number" v-model="form.num" placeholder="请输入红包数量" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getreadPacketList, postReadPacket, putReadPacketStatus } from "@/api/system/readPacket";

export default {
  name: "redPackets",
  dicts: ['redp_packets_type','redp_packets_status'],
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      
      title: '',
      open: false,
      // 查询参数
      queryParams: {
        code: undefined,
        endCreateDate: undefined,
        startCreateDate: undefined,
        status: undefined,
        type: undefined,
        pageNum: 1,
        pageSize: 10,
      },
      // 表单校验
      rules: {
        type: [
          { required: true, message: "请选择红包类型", trigger: "blur" },
        ],
        amt: [
          { required: true, message: "请输入红包金额", trigger: "blur" }
        ],
        num: [
          { required: true, message: "请输入红包数量", trigger: "blur" },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      getreadPacketList(this.queryParams).then(response => {
        this.list = response.data.rows;
        this.total = response.data.total || 0;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        type: undefined,
        num: undefined,
        amt: undefined,
      };
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新增红包";
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          postReadPacket(this.form).then(response => {
              this.$modal.msgSuccess("新增红包成功");
              this.open = false;
              this.getList();
            });
        }
      });
    },
      // 用户状态修改
      handleStatusChange(row) {
      let text = row.status === 1 ? "启用" : "关闭";
      this.$modal.confirm('确认要"' + text + '""' + row.code + '"红包吗？').then(function() {
        return putReadPacketStatus(row.id, row.status);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === 1 ? 2 : 1;
      });
    },
  }
};
</script>